<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <style>
        .login-form {
            max-width: 400px;
            margin: 50px auto;
        }

        .login-form img {
            max-width: 100%;
            height: auto;
            margin-bottom: 20px;
        }

        .login-form .form-group {
            margin-bottom: 20px;
        }

        .login-form .captcha {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .login-form .captcha img {
            margin-right: 10px;
        }

        .register-link {
            text-align: center;
        }
    </style>
    <title>登录</title>
</head>
<body>

<div class="container login-form">
    <h2 class="text-center">登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" class="form-control" name="username" id="username" placeholder="输入用户名" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" name="password" id="password" placeholder="输入密码" required>
        </div>

        <div class="form-group">
            <label for="type">身份</label>
            <select id="type" name="type" class="form-control">
                <option value="1">管理员</option>
                <option value="2">用户</option>
            </select>
        </div>

        <div class="form-group">
            <label for="captcha">验证码</label>
            <input type="text" name="captcha" class="form-control" id="captcha" placeholder="输入验证码" required>
            <img id="captchaImage" src="/code" alt="验证码">
        </div>
        <button type="button" id="loginButton" class="btn btn-primary btn-block">登录</button>
    </form>
    <div class="register-link">
        <p>还没有账号？<a href="register.html">注册</a></p>
    </div>
</div>

<script src="/js/jquery.min.js"></script>
<script>
    $(document).ready(function () {

        $("#loginButton").click(function () {
            var formData = $("#loginForm").serialize();
            $.ajax({
                type: "POST",
                url: "/login",
                data: formData,
                success: function (response) {
                    var typeValue = document.getElementById("type").value
                    console.log(typeValue)
                    if(typeValue == 1){
                        window.location.href = "/admin/user_index.html"
                    }else {
                        window.location.href = "/index.html"
                    }
                },
                error: function (error) {
                    console.log("Error:", error);
                }
            });
        });


        $("#captchaImage").click(function () {
            refreshCaptcha();
        });

        function refreshCaptcha() {
            $("#captchaImage").attr("src", "/code?" + new Date().getTime());
        }

    });
</script>




</body>
</html>
